/**
 * Copyright (c) 2012-2014, Yang Yang <cntototo@gmail.com>
 *
 * This software may be distributed under the terms of the New BSD License.
 * See README for more details.
 */

#ifndef LOG_H
#define LOG_H

#include "def.h"

/** Log level */
#define LOG_NONE	0x00000000
#define LOG_INFO	0x00000001
#define LOG_WARN	0x00000002
#define LOG_ERR		0x00000004
#define LOG_DEBUG	0x00000008
#define LOG_FLOW	0x00000010
#define LOG_DUMP	0x00000020
#define LOG_ALL		0xFFFFFFFF

#define DEFAULT_LOG	(LOG_NONE	\
			|LOG_INFO	\
			|LOG_WARN	\
			|LOG_ERR	\
)

#ifdef CONFIG_NO_STDOUT_DEBUG

#define wfs_printf(args...) do { } while (0)
#define ENTER() do { } while (0)
#define LEAVE() do { } while (0)
#define wfs_perror(str) do { } while (0)
#define wfs_hexdump(l,p,b,le) do { } while (0)
#define wfs_hexdump_ascii(l,p,b,le) do { } while (0)
#define wfs_event(args...) do { } while (0)
#define wfs_event_register_cb(f) do { } while (0)
#define wfs_print_line(args...) do { } while (0)

#else

#ifndef LOG_TAG
#define LOG_TAG "<?>"
#endif

#define wfs_printf(level, ...) print_log(level, LOG_TAG ": " __VA_ARGS__)
#define ENTER() wfs_printf(LOG_FLOW, "+ %s in %s:%d", __func__, \
			__FILE__, __LINE__)
#define LEAVE() wfs_printf(LOG_FLOW, "- %s in %s:%d", __func__, \
			__FILE__, __LINE__)
#define wfs_perror(str) perror(str)
#define wfs_hexdump(level, prefix, buf, len) \
		print_hexdump(level, LOG_TAG ": " prefix, buf, len, 0)
#define wfs_hexdump_ascii(level, prefix, buf, len) \
		print_hexdump(level, LOG_TAG ": " prefix, buf, len, 1)
#define wfs_print_line(level, ch) \
		print_line(level, LOG_TAG ": ", ch)

void set_log_level(u32 level);
void reset_log_level(void);
void print_log(u32 level, const char *fmt, ...);
void print_hexdump(u32 level, const char *prefix, const u8 *buf, u32 len,
		u8 ascii);
void wfs_event(void *ctx, u32 level, const char *fmt, ...);
void print_line(u32 level, const char *prefix, char ch);

typedef const char *(*wfs_event_get_ifname_func)(void *ctx);

void wfs_event_register_ifname_cb(wfs_event_get_ifname_func func);

typedef void (*wfs_event_cb_func)(void *ctx, u32 level, const char *buf,
		int len);

void wfs_event_register_cb(wfs_event_cb_func func);

#endif

#endif /* LOG_H */
